package com.yanqu.road.server.timer.cross3;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankListModel;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankListModel;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.cross3.GameXRankMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

public class GameXRankAllSyncTask extends GeneralTask {

    @Override
    public void doRun() {

        // 个人榜
        for (Map<Integer, XUserRankListModel> modelMap : GameXRankMgr.getUserRankMap().getUserRankMap().values()) {
            for (XUserRankListModel model : modelMap.values()) {
                // 在同步时间内，且没全同步过
                ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(model.getActivityId());
                if (activityInAllSyncPeriod(activityInfo) && model.getAllSyncTime() == 0) {
                    // 全同步
                    XRankProto.XUserRankListMsg.Builder allSyncMsg = XRankPb.parseXUserRankListMsg(
                            model.getActivityId(),
                            model.getConditionType(),
                            model.getAllRankList(),
                            false);
                    YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_UPLOAD_USER_RANK_FROM_GAME, allSyncMsg);
                    GamePlayerMgr.sendPacket(0, pbMessage);
                    // 修改全同步时间
                    model.setAllSyncTime(System.currentTimeMillis());
                }
            }
        }

        // 地区榜
        for (Map<Integer, XChannelRankListModel> modelMap : GameXRankMgr.getChannelRankMap().getChannelRankMap().values()) {
            for (XChannelRankListModel model : modelMap.values()) {
                // 在同步时间内，且没全同步过
                ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(model.getActivityId());
                if (activityInAllSyncPeriod(activityInfo) && model.getAllSyncTime() == 0) {
                    // 全同步
                    XRankProto.XChannelRankPartListMsg.Builder allSyncMsg = XRankPb.parseXChannelRankPartListMsg(
                            model.getActivityId(),
                            model.getConditionType(),
                            model.getPartValueRankList(ServerListMgr.getChannelId(GameServer.getInstance().getServerId())));
                    YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_UPLOAD_CHANNEL_RANK_PART_FROM_GAME, allSyncMsg);
                    GamePlayerMgr.sendPacket(0, pbMessage);
                    // 修改全同步时间
                    model.setAllSyncTime(System.currentTimeMillis());
                }
            }
        }

    }

    private static boolean activityInAllSyncPeriod(ActivityInfo info) {
        if (info == null) {
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return info.getEndTime() <= now && now <= info.getEndTime() + 4 * DateHelper.MINUTE_SECONDS; // 活动结束4分钟内
    }

}
